<?php 
	include 'db.php';
	session_start();
	
	function CleanDataJ($str){
		$str = mysql_real_escape_string($str);
		$str = strip_tags($str);
		return $str;
	}
	
	
	$receiptDate = CleanDataJ($_SESSION['receiptDate']);
	$proformaID = CleanDataJ($_SESSION['proformaID']);
	$custName = CleanDataJ($_SESSION['name']);
	$custJobTitle = CleanDataJ($_SESSION['jobtitle']);
	$custLocation = CleanDataJ($_SESSION['location']);
	$custPO = CleanDataJ($_SESSION['PO']);
	$custNumb = CleanDataJ($_SESSION['number']);
	$custEmail = CleanDataJ($_SESSION['email']);
	$salesman = CleanDataJ($_SESSION['salesName']);
	$payterms = CleanDataJ(@$_POST['payterms']);
	$delivery = CleanDataJ(@$_POST['delivery']);
	$warranty = CleanDataJ(@$_POST['warranty']);
	$remarks = CleanDataJ(@$_POST['remarks']);
	
	if(isset($_SESSION['discPrice'])){
		$discPrice = $_SESSION['discPrice'];
	}else{
		$discPrice = 0;
	}
	$totalPrice = $_SESSION['totalCost'];
	$iyot = "INSERT INTO receipts(receiptDate, transacID, proformaID, custName, custJobTitle, custLocation, custPO, custNumb, custEmail, salesman, discPrice, totalPrice, payment_terms, delivery, warranty, remarks, customerID) values('$receiptDate', NULL, '$proformaID', '$custName', '$custJobTitle', '$custLocation', '$custPO', '$custNumb', '$custEmail', '$salesman', '$discPrice', '$totalPrice', '$payterms', '$delivery', '$warranty', '$remarks', ".$_SESSION['cID'].")";
	//$_SESSION['customerID']
	 mysql_query($iyot);
	$receiptID = mysql_insert_id();
	
	$allitems = mysql_query("SELECT itemId FROM items");
	while($item_order = mysql_fetch_array($allitems)){
		if($item_order['itemId'] == $_SESSION['item_'.$item_order['itemId']]){	
			$qty = $_SESSION['item_'.$item_order['itemId'].'_qty'];
			mysql_query("INSERT INTO `order` SET receipt_id = $receiptID, customer_id = ".$_SESSION['cID'].", item_id = ".$_SESSION['item_'.$item_order['itemId']].", qty = $qty, `date` = now()");
			$_SESSION['item_'.$item_order['itemId'].'_qty'] = NULL;
			$_SESSION['item_'.$item_order['itemId']] = NULL;
			
		}
	}
	
	function generatePDF(){
		require_once('inc/tcpdf/config/lang/eng.php');
		require_once('inc/tcpdf/tcpdf.php');
		
		// create new PDF document
			$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
			
			// set document information
			$pdf->SetCreator(PDF_CREATOR);
			$pdf->SetAuthor('Nicola Asuni');
			$pdf->SetTitle('TCPDF Example 061');
			$pdf->SetSubject('TCPDF Tutorial');
			$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
			
			// set default header data
			$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
			
			// set header and footer fonts
			$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
			$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
			
			// set default monospaced font
			$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
			
			//set margins
			$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
			$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
			$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
			
			//set auto page breaks
			$pdf->SetAutoPageBreak(true, PDF_MARGIN_BOTTOM);
			
			//set image scale factor
			$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
			
			//set some language-dependent strings
			$pdf->setLanguageArray($l);
			
			// ---------------------------------------------------------
			
			// set font
			$pdf->SetFont('helvetica', '', 10);
			
			// add a page
			$pdf->AddPage();
			
			/* NOTE:
			 * *********************************************************
			 * You can load external XHTML using :
			 *
			 * $html = file_get_contents('/path/to/your/file.html');
			 *
			 * External CSS files will be automatically loaded.
			 * Sometimes you need to fix the path of the external CSS.
			 * *********************************************************
			 */
			
			// define some HTML content with style 
			
			$pdfOutput = $_SESSION['pdfOutput'].'<br /><strong>Validity:</strong> 15 Days
			<br /><strong>Payment Terms:</strong> '.@$_POST['payterms'].'
			<br /><strong>Warranty:</strong> '.@$_POST['warranty'].'
			<br /><strong>Delivery:</strong> '.@$_POST['delivery'].'
			<br /><strong>Remarks:</strong> '.@$_POST['remarks'].'
			<br /><strong>Bank Details:</strong> Cheque to be made on Anton W.C. Denker GMBH FZE</p>
			<p><br/></p>
			<p><br/></p>
			<p><br/></p>
			<p><br/></p>
			<table width="100%" >
				<tr>
					<td width="35%">
					By Buyer<br/><br/>
					___________________________
					</td>
					<td width="15%"></td>
					<td width="15%"></td>
					<td width="35%">
					For Anton W.C. Denker GMBH FZE<br/><br/>
					___________________________
					</td>
				</tr>
			</table>
			<p><br/></p>
			<p><br/></p>
			<p><br/></p>
			<p><br/></p>
			<p>P.O. Box - 16111</p>
			<p>Ras AI Khaimah, UAE</p>
			<p>Tel - +97143535455</p>
			<p>Fax - +9714 3534283</p>
			<p>Web - http://www.ledlampsuae.com</p>
			<p>Email -admin@ledlampsuae.com</p>
			';
			
			// output the HTML content
			$pdf->writeHTML($_SESSION['pdfHeading'], true, false, true, false, '');
			$pdf->writeHTML($pdfOutput, true, false, true, false, '');
			//$pdf->writeHTML($html, true, false, true, false, '');
			// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
			
			// *******************************************************************
			// HTML TIPS & TRICKS
			// *******************************************************************
			
			// REMOVE CELL PADDING
			//
			// $pdf->SetCellPadding(0);
			// 
			// This is used to remove any additional vertical space inside a 
			// single cell of text.
			
			// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
			
			// REMOVE TAG TOP AND BOTTOM MARGINS
			//
			// $tagvs = array('p' => array(0 => array('h' => 0, 'n' => 0), 1 => array('h' => 0, 'n' => 0)));
			// $pdf->setHtmlVSpace($tagvs);
			// 
			// Since the CSS margin command is not yet implemented on TCPDF, you
			// need to set the spacing of block tags using the following method.
			
			// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
			
			// SET LINE HEIGHT
			//
			// $pdf->setCellHeightRatio(1.25);
			// 
			// You can use the following method to fine tune the line height
			// (the number is a percentage relative to font height).
			
			// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
			
			// CHANGE THE PIXEL CONVERSION RATIO
			//
			// $pdf->setImageScale(0.47);
			// 
			// This is used to adjust the conversion ratio between pixels and 
			// document units. Increase the value to get smaller objects.
			// Since you are using pixel unit, this method is important to set the
			// right zoom factor.
			// 
			// Suppose that you want to print a web page larger 1024 pixels to 
			// fill all the available page width.
			// An A4 page is larger 210mm equivalent to 8.268 inches, if you 
			// subtract 13mm (0.512") of margins for each side, the remaining 
			// space is 184mm (7.244 inches).
			// The default resolution for a PDF document is 300 DPI (dots per 
			// inch), so you have 7.244 * 300 = 2173.2 dots (this is the maximum 
			// number of points you can print at 300 DPI for the given width).
			// The conversion ratio is approximatively 1024 / 2173.2 = 0.47 px/dots
			// If the web page is larger 1280 pixels, on the same A4 page the 
			// conversion ratio to use is 1280 / 2173.2 = 0.59 pixels/dots
			
			// *******************************************************************
			
			// reset pointer to the last page
			//$pdf->lastPage();
			
			// ---------------------------------------------------------
			//Change To Avoid the PDF Error 
  			ob_end_clean();
			//Close and output PDF document
			return $pdf->Output('ledlamps-invoice.pdf', 'D');
			
			//session_destroy();
			//header("Location: receiptSaved.html");
			//============================================================+
			// END OF FILE                                                
			//============================================================+

	}
	//destroy session
	generatePDF();
	
	
	
	
?>